package com.oocl.o2o.client.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.xml.registry.infomodel.EmailAddress;

import org.springframework.stereotype.Repository;

import com.oocl.o2o.client.dao.ReceiverInfoDao;
import com.oocl.o2o.client.entity.ReceiverInfo;

/**
 * 
 * @author HEHA4
 *
 */

@Repository(value="receiverInfoDaoImpl")
public class ReceiverInfoDaoImpl implements ReceiverInfoDao {

	@PersistenceContext(name="un")
	private EntityManager manager;
	
	@Override
	public ReceiverInfo add(ReceiverInfo r) {
		manager.persist(r);
		
		return r;
	}

	@Override
	public ReceiverInfo update(ReceiverInfo r) {
		ReceiverInfo receiverInfo = manager.find(ReceiverInfo.class, r.getId());
		
		manager.clear();
		manager.merge(r);
		return r;
	}

	@Override
	public ReceiverInfo delete(String id) {
		ReceiverInfo receiverInfo = manager.find(ReceiverInfo.class, id);
		System.out.println("receiverInfo  "+receiverInfo);
		
		
		receiverInfo.setCustomer(null);
		update(receiverInfo);
		return receiverInfo;
	}

	@Override
	public ReceiverInfo findById(String id) {
		ReceiverInfo receiverInfo = manager.find(ReceiverInfo.class, id);
		return receiverInfo;
	}

	@Override
	public List<ReceiverInfo> findByCustomerId(String id) {
		String jpql = "select r from ReceiverInfo r where r.customer.id =:id ";
		List<ReceiverInfo> list = manager.createQuery(jpql).setParameter("id", id).getResultList();
		
		return list;
	}

}
